package ink.fujisann.learning.stock.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import ink.fujisann.learning.base.SortConst;
import ink.fujisann.learning.stock.pojo.StockDebt;
import ink.fujisann.learning.stock.resp.DayIncomeResp;
import ink.fujisann.learning.stock.service.MyIncomeService;
import ink.fujisann.learning.stock.service.StockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Method;
import java.util.List;

@ApiSort(SortConst.stockSort)
@RequestMapping("/stock")
@RestController
@Slf4j
@Api(tags = {"股票数据入库"}, description = "StockController")
public class StockController {

    private StockService stockService;

    @Autowired
    public void setStockService(StockService stockService) {
        this.stockService = stockService;
    }

    @SneakyThrows
    @ApiOperation("构建数据")
    @GetMapping("/build")
    public void build(@RequestParam String methodName) {
        Class<StockService> aClass = StockService.class;
        Method method = aClass.getDeclaredMethod(methodName);
        method.invoke(stockService);
    }

    @ApiOperation("插入指定日期的数据")
    @GetMapping("/tradeDate")
    public void tradeDate(@RequestParam String date) {
        stockService.todayTrade(date);
    }

    @ApiOperation("查询新股票")
    @GetMapping("/addNewStock")
    public void addNewStock() {
        stockService.addNewStock();
    }

    @ApiOperation("添加假期")
    @GetMapping("/addHoliday")
    public void addHoliday() {
        stockService.addHoliday();
    }

    private MyIncomeService myIncomeService;

    @Autowired
    public void setMyIncomeService(MyIncomeService myIncomeService) {
        this.myIncomeService = myIncomeService;
    }

    @ApiOperation("我的每日收入")
    @GetMapping("/myIncome")
    public List<DayIncomeResp> myIncome() {
        return myIncomeService.myIncome();
    }

    @ApiOperation("赌约分页查询")
    @GetMapping("/debtPage/{page}/{size}")
    public Page<StockDebt> debtPage(@PathVariable int page, @PathVariable int size) {
        return stockService.debtPage(page, size);
    }

    @ApiOperation("新增/修改赌约")
    @PostMapping("/saveDebt")
    public void saveDebt(@RequestBody StockDebt stockDebt) {
        stockService.addDebt(stockDebt);
    }

    @ApiOperation("删除赌约")
    @DeleteMapping("/deleteDebt/{debtId}")
    public void deleteDebt(@PathVariable String debtId) {
        stockService.deleteDebt(debtId);
    }
}
